﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace BuildTreeViewFromFormattedList
{
    public static class TreeViewConverter
    {
        public static TreeNode buildNodeList(TreeNode root, String nodePath, Char pathDelimiter)
        {
            if (null == root)
            {
                return null;
            }

            char[] separators = new char[1];
            separators[0] = pathDelimiter;
            String[] nodes = nodePath.Split(separators);
            int countOfNodes = nodes.Count();
            TreeNode childNode = root;

            for (int i = 0; i < countOfNodes; i++)
            {
                if (i == 0)
                {
                    if (!nodes[i].Equals(childNode.Name))
                    {
                        return null;
                    }
                    else
                    {
                        continue;
                    }
                }

                Boolean f_searchNextChild = false;
                foreach (TreeNode children in childNode.Nodes)
                {
                    if (children.Name.Equals(nodes[i]))
                    {
                        childNode = children;
                        f_searchNextChild = true;
                        break;
                    }
                }

                if (f_searchNextChild)
                {
                    continue;
                }
                else
                {

                    int remainingNodes = countOfNodes - i;
                    TreeNode[] newNodes = new TreeNode[remainingNodes];

                    if (remainingNodes > 0)
                    {
                        for (int j = remainingNodes - 1; j >= 0; j--)
                        {
                            newNodes[j] = new TreeNode(nodes[j + i]);
                            newNodes[j].Name = nodes[j + i];
                            if (j < remainingNodes - 1)
                            {
                                newNodes[j].Nodes.Add(newNodes[j + 1]);
                            }
                        }

                        childNode.Nodes.Add(newNodes[0]);
                        return root;

                    }
                }
            }

            return root;

        }

        public static void buildTreeView(TreeNodeCollection treeViewNodeCollection, DataTable dt, Char separator, String treeDataField)
        {
            if (null == dt || dt.Rows.Count == 0)
            {
                return;
            }

            TreeNode rootNode = new TreeNode();

            foreach (DataRow row in dt.Rows)
            {
                String nodePath = row.Field<String>(treeDataField);

                if (!String.IsNullOrEmpty(nodePath))
                {

                    String startNode = nodePath.Substring(0, nodePath.IndexOf('/'));
                    rootNode.Name = startNode;
                    rootNode.Text = startNode;

                    if (treeViewNodeCollection.Count == 0)
                    {
                        rootNode = buildNodeList(rootNode, nodePath, separator);
                        treeViewNodeCollection.Add(rootNode);
                    }
                    else
                    {
                        Boolean f_foundRoot = false;
                        foreach (TreeNode root in treeViewNodeCollection)
                        {
                            if (root.Text.Equals(startNode))
                            {
                                f_foundRoot = true;
                                buildNodeList(root, nodePath, separator);
                            }
                        }
                        if (f_foundRoot == false)
                        {
                            rootNode = buildNodeList(rootNode, nodePath, separator);
                            treeViewNodeCollection.Add(rootNode);
                        }

                    }

                }

            }


        }

    }
}
